package com.streamkar.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.streamkar.common.config.session.UserSession;
import com.streamkar.constant.Constants;
import com.streamkar.constant.ReturnCode;
import com.streamkar.entity.base.Response;
import com.streamkar.entity.base.ResponseData;
import com.streamkar.entity.domain.Agency;
import com.streamkar.entity.domain.Talent;
import com.streamkar.entity.request.TalentRequest;
import com.streamkar.service.AgencyService;
import com.streamkar.service.TalentService;
import com.streamkar.util.EsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @ClassName AgencyRest
 * @Description
 * @Author tyfrost
 * @Date 2018/11/13 下午11:50
 * @Version 1.0
 */
@RestController
public class AgencyRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(AgencyRest.class);

    @Autowired
    TalentService talentService;

    @Autowired
    AgencyService agencyService;

    @Autowired
    EsUtil esUtil;

    @Autowired
    UserSession userSession;


    @RequestMapping(value = "/agency/info", method = RequestMethod.GET)
    @ResponseBody
    public Response agencyDetail(HttpServletRequest request) {
        ResponseData<Agency> response = new ResponseData<>();

        if (null == userSession.getUserId()) {
            response.setCode(ReturnCode.SESSION_TIMEOUT);
            return response;
        }
        // 查询
        Agency agency = agencyService.agencyInfo(userSession.getUserId());

        // 代理不为空不为空则继续代理统计信息
        if (null != agency) {
            JSONArray agencyStat = agencyService.agencyStat(agency.getAgencyId());
            if (!CollectionUtils.isEmpty(agencyStat)) {
                agency.setAgencyStat(agencyStat);
            }
        }
        response.setData(agency);
        return response;
    }

    @RequestMapping(value = "/agency/quota", method = RequestMethod.GET)
    @ResponseBody
    public Response agencyQuota() {
        ResponseData<JSONObject> response = new ResponseData<>();
        if (null == userSession.getUserId()) {
            response.setCode(ReturnCode.SESSION_TIMEOUT);
            return response;
        }
        // 查询
        Agency agency = agencyService.agencyInfo(userSession.getUserId());

        // 代理不为空不为空则继续代理统计信息
        if (null != agency) {
            JSONObject agencyStat = agencyService.agencyQuota(agency.getAgencyId());
            response.setData(agencyStat);
        }
        return response;
    }


    @RequestMapping(value = "/agency/talent", method = RequestMethod.GET)
    @ResponseBody
    public Response agencyTalentList(TalentRequest talentRequest, HttpServletRequest request) {
        ResponseData<Object> response = new ResponseData<>();
        Integer agencyId = userSession.getUserId();
        if (null == agencyId) {
            response.setCode(ReturnCode.SESSION_TIMEOUT);
            return response;
        }
        Talent talent = talentRequest.toEvent();
        talent.setAgencyId(agencyId);

        // 主播等级 1:红星主播 1-6 2：钻石主播 7-11 3：皇冠 12-55
        Integer talentLevel = talentRequest.getTalentLevel();
        int startLevel = Constants.START_LEVEL;
        int endLevel = Constants.END_LEVEL;
        if (Objects.equals(talentLevel, new Integer(1))) {
            endLevel = 6;
        } else if (Objects.equals(talentLevel, new Integer(2))) {
            startLevel = 7;
            endLevel = 11;
        } else if (Objects.equals(talentLevel, new Integer(3))) {
            startLevel = 12;
        }
        response.setData(talentService.talentList(talent, startLevel, endLevel, talentRequest.getQueryValue()));
        return response;
    }
}
